package com.hdoit.kylin.web.user.controllers;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hdoit.kylin.logical.user.model.Student;
import com.hdoit.kylin.logical.user.model.Teacher;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.BaseController;

@Controller
@RequestMapping(value="/admin/user/personal/")
public class PersonalController extends BaseController{
	@Override
	public String getRoutePath() {
		return "areas/admin/user/personal/";
	}
	
	private ITeacherService teacherService;
	private IStudentService studentService;

	public ITeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public IStudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	private final String myInfoViewName="myInfo";
	private final String saveTeacher="saveTeacher";
	private final String saveStudent="saveStudent";
	
	@RequestMapping(value=myInfoViewName,method={RequestMethod.GET})
	public String myInfo(Model model){
		String employeeId=getLoginModel().getLoginId();
		
		Teacher teacher=teacherService.queryModel(employeeId);
		if(null!=teacher){
			model.addAttribute("teacher", teacher);
			return view("teacher");
		}
		
		Student student=studentService.queryModel(employeeId);
		if(null!=student){
			model.addAttribute("student", student);
			return view("student");
		}
		
		return "";
	}
	
	@RequestMapping(value=saveTeacher,method={RequestMethod.POST})
	public @ResponseBody String saveTeacher(Model model,Teacher teacher){
		teacher.setEmployeeName(teacher.getEmployeeAccount());
		teacher.setModifyDate(DateUtils.getCurrent());
		teacher.setModifyUserId(getLoginModel().getLoginId());
		teacher.setModifyUserName(getLoginModel().getLoginName());
		teacher.setEmployeeCode(ChineseUtils.getPingYin(teacher.getTeacherName()));
		teacher.setMarkCode(ChineseUtils.getPinYinHeadChar(teacher.getTeacherName()));
		teacher.setModelStatus(ModelStatus.update);
		
		String result;
		if(teacherService.applyChange(teacher)){
			result="success";
		}
		else{
			result="failed";
		}
		
		return result;
	}
	
	@RequestMapping(value=saveStudent,method={RequestMethod.POST})
	public @ResponseBody String saveStudent(Model model,Student student){
		student.setEmployeeName(student.getEmployeeAccount());
		student.setModifyDate(DateUtils.getCurrent());
		student.setModifyUserId(getLoginModel().getLoginId());
		student.setModifyUserName(getLoginModel().getLoginName());
		student.setEmployeeCode(ChineseUtils.getPingYin(student.getStudentName()));
		student.setMarkCode(ChineseUtils.getPinYinHeadChar(student.getStudentName()));
		student.setModelStatus(ModelStatus.update);
		
		String result;
		if(studentService.applyChange(student)){
			result="success";
		}
		else{
			result="failed";
		}
		
		return result;
	}
}
